import time


def add_1h_keys(x, y):
    ret = []
    for_list = list(range(len(x)))
    removelist = []
    for i in range(len(x)-1):
        if i not in removelist:
            tmp = [i, ]
            for j in range(i+1, len(x)):
                if is_eq_time(x[i], x[j]):
                    tmp.append(j)
                    removelist.append(j)
                else:
                    pass
            ret.append(tmp)
    return ret


def time_h(t1):
    """传入一个时间字符串返回当前小时 + 1"""
    # print(t1)
    struct_time1 = time.strptime(t1, "%Y-%m-%d %H:%M:%S")
    # print(struct_time1)
    struct_time_ret = list(time.strptime(
        "2018-06-12 00:00:00", "%Y-%m-%d %H:%M:%S"))
    # print(struct_time_ret)
    for i in range(4):
        struct_time_ret[i] = struct_time1[i]
    # print(struct_time_ret)

    ret = str(struct_time_ret[0]) + "-"+str(struct_time_ret[1])+"-" + \
        str(struct_time_ret[2]) + " " + str(struct_time_ret[3] + 1) + ":00:00"
    # ret = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    return ret


def is_eq_time(t1, t2):
    """传入两个时间字符串判断是否为同一小时"""
    # t1 = time.mktime(time.strptime(t1,"%Y-%m-%d %H:%M:%S"))
    # t2 = time.mktime(time.strptime(t2,"%Y-%m-%d %H:%M:%S"))
    # print(t1, t2)
    struct_time1 = time.strptime(t1, "%Y-%m-%d %H:%M:%S")
    struct_time2 = time.strptime(t2, "%Y-%m-%d %H:%M:%S")
    # print(struct_time1,struct_time2)
    for i in range(4):
        if struct_time1[i] != struct_time2[i]:
            return False
    return True


def add_1h_vals(x, y):
    """返回每个小时的交易总数"""
    # print(len(x),len(y))
    if len(x) != len(y):
        return -1
    if (len(x) < 2) or (len(y) < 2):
        return -1
    tmplist = add_1h_keys(x, y)
    h = []
    v = []
    for i in tmplist:
        h.append(time_h(x[i[0]]))
        vvv = 0
        for j in i:
            vvv += y[j]
        v.append(vvv)
    return h, v


def main():
    x1 = ["2018-06-12 15:00:00", "2018-06-12 15:16:36", "2018-06-12 19:10:41", "2018-06-13 11:20:16", "2018-06-13 14:25:39",
          "2018-06-14 16:04:21", "2018-07-14 16:08:40", "2018-06-14 13:07:01", "2018-06-14 13:07:01", "2018-06-12 15:07:01"]
    y1 = [1, 4, 544, 6, 7, 833, 9, 10, 302, 0]
    print(add_1h_vals(x1, y1))


if __name__ == '__main__':
    main()
